<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%@ taglib prefix="d" uri="http://dgg.net/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <title></title>
    <c:import url="../../header.jsp"/>
</head>
<body>
<div class="content-warp">
    <div class="dgg-list-top-warp">
        <input type="hidden" value="${isChangeLevel}" id="isChangeLevel">
        <input type="hidden" value="${isChangeSort}" id="isChangeSort">
        <div class="screen-search">
            <ul>
                <li>
                    <label>员工姓名：</label>
                    <input type="text" class="dggui-input dggui-input-normal" name="userName" id="userName"
                           placeholder="输入员工姓名">
                </li>
               <%-- <li>
                    <label>团队：</label>
                    <select id="teamId" name="teamId"  class="dggui-select">
                        <option value=''>请选择</option>
                    </select>
                </li>--%>
                <li>
                    <button class="dggui-btn searchBtn" onclick="tableReload();">搜索</button>
                    <button class="dggui-btn dggui-btn-primary" onclick="cleanSearch();">清空条件</button>
                    <button class="dggui-btn dggui-btn-green"
                            onclick="excelManagementIndex('${basePath}/rzsc/royalty_management/excel_management_index.do');">导出
                    </button>
                </li>
            </ul>
        </div>
    </div>
    <div class="list-content">
        <div class="tool-bar">
            <div class="fl">
            </div>
        </div>
        <div class="table-warp">
            <table class="dggui-table" id="managementList">
            </table>
        </div>
    </div>
</div>

<c:import url="/WEB-INF/jsp/footer.jsp"/>
<script>
    var tables;
    var select2;
    var tableName = "managementList";
    dggui.use(['table','select2'], function () {
        select2 = dggui.select2;
        var isChangeLevel = $("#isChangeLevel").val();
        var isChangeSort = $("#isChangeSort").val();
        var table = dggui.table;
        var opt = {
            "elem": "#managementList",
            "ajax": sysInfo.basePath + "/rzsc/royalty_management/management_list.do",
            "scrollX": "true",//是否开启横项滚动
            "oneSelectCheck": true,//单选，同时选中复选框
            "fixedColumns": {//锁定列，只有开启scrollX时才生效
                "leftColumns": 1,//左侧锁定2列
                "rightColumns": 1//右侧锁定1列
            },
            "visible": true,
            "fnServerParams": function (aoData) {
                //搜索条件
                aoData = $.extend(aoData, {
                        userName: $("#userName").val()
                    }
                )
                return aoData;
            },
            "aoColumns": [
                {
                    "title": "姓名",
                    "data": "realName",
                    "orderable": false,
                    "render": function (data, type, full, meta) {
                        return data ? data : '';
                    }
                },
                {
                    "title": "工号",
                    "data": "loginName",
                    "orderable": false,
                    "render": function (data, type, full, meta) {
                        return data ? data : '';
                    }
                },
                {
                    "title": "部门",
                    "data": "orgName",
                    "orderable": false,
                    "render": function (data, type, full, meta) {
                        return data ? data : '';
                    }
                },
                {
                    "title": "当前级别",
                    "data": "curLevelStr",
                    "orderable": false,
                    "render": function (data, type, full, meta) {
                        return data ? data : '';
                    }
                },
                {
                    "title": "上次级别",
                    "data": "oldLevel",
                    "orderable": false,
                    "render": function (data, type, full, meta) {
                        return data ? data : '';
                    }
                },
                {
                    "title": "优先级",
                    "data": "priorityStr",
                    "orderable": false,
                    "render": function (data, type, full, meta) {
                        return data ? data : '';
                    }
                }
                ,{
                    'title': '操作',
                    "orderable": false,
                    "render": function(data, type, row, meta) {
                        var htm = '';
                        var isChangeLevelHtm = '<a href="javascript:void(0)" class="dggui-link" onclick="changeLevel(\''+row.userId+'\',\''+row.curLevel+'\',\''+row.curLevelStr+'\');">调整级别</a>';
                        var isChangeSortHtml = '&nbsp;&nbsp;<a href="javascript:void(0)" class="dggui-link" onclick="changeSort(\''+row.userId+'\',\''+row.priorityStr+'\')">调整优先级</a>';
                        if(isChangeLevel=='true'){
                            htm += isChangeLevelHtm;
                        }
                        if(isChangeSort=='true'){
                            htm += isChangeSortHtml;
                        }
                        return htm;
                    }
                }

            ]
        };
        tables = table.render(opt);
        // 处理dataTable 异常错误信息
        $.fn.dataTable.ext.errMode = 'none';//不抛出异常
        tables.on('error.dt', function (e, settings, techNote, message) {
            //dggui.dialog.errors("表格查询错误，请联系管理员", 2000);
            if (checkEmpty(settings.jqXHR.responseJSON.error)) {
                dggui.dialog.errors(settings.jqXHR.responseJSON.error, 2000);
            }
        });
    });

    /**
     * 调整级别
     * @param userId
     * @param curLevel
     * @param curLevelStr
     */
    function changeLevel(userId,curLevel,curLevelStr) {
        $.ajax({
            type: 'POST',
            async: false,
            url: sysInfo.basePath + "/rzsc/royalty_management/change_level.html",
            data:{userId:userId,curLevel:curLevel,curLevelStr:curLevelStr},
            success: function (resHtml) {
                dggui.dialog.open({
                    title:'调整级别',
                    content: resHtml,
                    area: '500px',
                    success:function(layero,index){
                        dggui.dialog.loading('close');
                    },
                    //点击确定触发
                    yes: function (index, layero) {
                        var level = $("#levelHid").val();
                        if(isEmpty(level)){
                            dggui.dialog.tip('请选择级别',$("#levelHid"));
                            return;
                        }
                        dggui.dialog.loading();
                        $.ajax({
                            type: 'POST',
                            data: {userId:userId,level:level},
                            url: sysInfo.basePath + "/rzsc/royalty_management/change_level_save.do",
                            dataType: 'JSON',
                            success: function (data) {
                                dggui.dialog.loading('close');
                                if(data.code == 0 ){//保存成功
                                    dggui.dialog.succeed(data.msg,2000);
                                    layer.close(index);//关闭弹框
                                    if(isEmpty(tableName)){
                                        window.location.reload();
                                    }else{
                                        reloadTable("#"+tableName,"reload");//刷新分页
                                    }
                                }else{
                                    dggui.dialog.errors(data.msg);
                                }
                            },
                            error: function () {
                                dggui.dialog.loading('close');
                                dggui.dialog.errors("发生未知错误，请联系管理员");
                            }
                        });

                    }
                })
            }
        })
    }
    /**
     * 调整优先级
     * @param userId
     * @param curLevel
     * @param curLevelStr
     */
    function changeSort(userId,priorityStr) {
        var sortArr = [];
        $.ajax({
            type: 'POST',
            async: false,
            url: sysInfo.basePath + "/rzsc/royalty_management/change_sort.html",
            data:{userId:userId,priorityStr:priorityStr},
            success: function (resHtml) {
                dggui.dialog.open({
                    title:'调整优先级',
                    content: resHtml,
                    area: '500px',
                    success:function(layero,index){
                        $(layero).find('.list-tag').click(function () {
                            var resList={};
                            var isActive=$(this).hasClass('active');
                            if(isActive) {
                                $(this).removeClass('active');
                                var id = $(this).data("id");
                                for(var i=0;i<sortArr.length;i++){
                                    if(id==sortArr[i].id){
                                        sortArr.splice(i,1);
                                    }
                                }
                            }else {
                                $(this).addClass('active');
                                resList.id=$(this).data("id");
                                resList.name=$(this).text();
                                sortArr.push(resList);
                            }
                            reSort(sortArr);
                        })
                    },
                    //点击确定触发
                    yes: function (index, layero) {
                       /* if(sortArr.length==0){
                            dggui.dialog.errors('请选择优先级');
                            return;
                        }*/
                        dggui.dialog.loading();
                        $.ajax({
                            type: 'POST',
                            data: {userId:userId,sortArr:sortArr},
                            url: sysInfo.basePath + "/rzsc/royalty_management/change_sort_save.do",
                            dataType: 'JSON',
                            success: function (data) {
                                dggui.dialog.loading('close');
                                if(data.code == 0 ){//保存成功
                                    dggui.dialog.succeed(data.msg,2000);
                                    layer.close(index);//关闭弹框
                                    if(isEmpty(tableName)){
                                        window.location.reload();
                                    }else{
                                        reloadTable("#"+tableName,"reload");//刷新分页
                                    }
                                }else{
                                    dggui.dialog.errors(data.msg);
                                }
                            },
                            error: function () {
                                dggui.dialog.loading('close');
                                dggui.dialog.errors("发生未知错误，请联系管理员");
                            }
                        });

                    }
                })
            },
            error:function (jqXHR, textStatus, errorThrown) {
                dggui.dialog.errors("获取优先级异常")
            }
        })
    }

    //列表刷新
    function tableReload() {
        tables.ajax.reload();
    }

    // 清空搜索
    function cleanSearch() {
        var $s = $('.screen-search');
        $s.find('input').val('');
        $s.find('select').val('').trigger("change");
    }

    //重新排列优先级
    function reSort(sortArr) {
        if(sortArr.length>0){
            var len = sortArr.length;
            var sortName = "";
            for(var i=0;i<sortArr.length;i++){
                sortName += sortArr[i].name;
                sortName += ">";
            }
            $("#sortName").text(sortName.substr(0,sortName.length-1));
        }
    }
//导出流程人员提成
    function excelManagementIndex(url){
        var obj = new Object();
        obj.userName=$("#userName").val();
        obj.type=1;
        formSubmit(url, obj);
    }
</script>
</body>
</html>